package com.syl.starter.itools.base.utils;

import org.slf4j.Logger;

/**
 * 日志打印优化工具类
 *
 * @author syl
 * @create 2018-09-07 13:39
 **/
public class LogUtils {

    public static void error(Logger logger, Throwable e){
        error(logger, null, e);
    }

    /**
     * 打印错误日志
     * @param logger
     * @param message
     * @param e 异常
     */
    public static void error(Logger logger, String message, Throwable e){
        error(logger,message,e,(Object) null);
    }

    /**
     * 打印错误日志 可使用{}占位符
     * @param logger
     * @param message
     * @param e
     * @param objects 占位符内容
     */
    public static void error(Logger logger, String message, Throwable e, Object... objects){
        logger.error(message,objects);
        if(e == null)
            return;
        // Print our stack trace
        logger.error(e.toString());
        for(StackTraceElement traceElement:e.getStackTrace())
            logger.error(traceElement.toString());
        // Print suppressed exceptions, if any
        for (Throwable throwable : e.getSuppressed())
            logger.error(throwable.toString());
        // Print suppressed exceptions, if any
        Throwable ourCause = e.getCause();
        if (ourCause != null)logger.error(ourCause.toString());
    }

}
